<?php
namespace app\api\controller;
use app\parent\api;
use think\Db;
use think\facade\Cache;

class Broker extends Api{
    public function broker(){

        $where = $this->whereSql([
            'like'=>['name'],
            'time' => ['time'],
            'eq' => ['cate_show'],
        ]);

        $data = Db::name('house_broker')
            ->where('cate_show',1)
            ->field('name,phone,desc,pic')
            ->order('id desc')
            ->select();
        // $data['desc'] = strip_tags($data['desc']);
        if($data){
            return $this->ajaxReturn($data);
        }
    }

    public function brokerS(){
        $where = $this->whereSql([
            'like'=>['name'],
            'time' => ['time'],
            'eq' => ['cate_show'],
        ]);

        $data = Db::name('house_broker')
            ->where($where)
            ->where('cate_show',1)
            ->select();
        if($data){
            return $this->ajaxReturn($data);
        }
    }

    /**
    * showdoc
    * 组成列表页查询条件
    * like , time , eq = 三个条件类型
    * ['like' => ['key1','key2']]
    */
    public function whereSql($arr)
    {
        $where = [];
        $info = $this->input();

        if(isset($arr['eq']) && is_array($arr['eq'])){

            foreach($arr['eq'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $where[] = $v . '=' . $info[$v];
                }
            }

        }
        
        if(isset($arr['like']) && is_array($arr['like'])){
            
            foreach($arr['like'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $where[] = $v . " like '%{$info[$v]}%'";
                }
            }

        }
        
        if(isset($arr['time']) && is_array($arr['time'])){
            
            foreach($arr['time'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $d = $this->whereSqlDate($info[$v]);
                    $where[] = "{$v} >= {$d[0]} and {$v} <= {$d[1]}";
                }
            }
        
        }

        if($where){
            $where = implode(' and ',$where);
        }else{
            $where = '';
        }

        return $where;
    }
}